package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SendCodeService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.vo.OrderSubmitParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author: liangWeiQUQ
 * @Description: 预约提交控制器
 * @DateTime: 2021/3/4 10:58
 **/
@RequestMapping("mobile/order")
@RestController
@Slf4j
public class OrderController {
    @DubboReference
    private MemberService memberService;
    @DubboReference
    private OrderService orderService;
    @DubboReference
    private SendCodeService sendCodeService;

    /**
     * @Author: 传闻中的梁大侠
     * @Date: 11:20 2021/3/4
     * @Parms [param]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 提交预约信息实现功能
     */
    @RequestMapping("/submit")
    public Result submit(@RequestBody OrderSubmitParam param) {
        log.info("[提交预约信息]data：{}", param);
        //验证码校验
        boolean check = sendCodeService.checkCode("ORDER", param.getTelephone(), param.getValidateCode());
        if (!check) {//如果验证码没有通过
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }
        //提交预约的时候如果不是会员就创建会员
        //1.创建会员
        Member member = new Member();
        member.setIdCard(param.getIdCard());
        member.setPhoneNumber(param.getTelephone());
        member.setName(param.getName());
        member.setSex(param.getSex());
        member = memberService.addIfNotExist(member);
        //2.提交体检信息
        Order order = new Order();
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(param.getSetmealId());
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderDate(param.getOrderDate());
        order.setMemberId(member.getId());
        order = orderService.add(order);
        //4-返回结果
        return new Result(true, MessageConst.ORDER_SUCCESS, order);
    }
    /**
       * @Author: 传闻中的梁大侠 
       * @Date: 16:14 2021/3/5
       * @Parms [id]
       * @ReturnType: com.itheima.health.entity.Result
       * @Description: 根据id查询预约订单的信息
     */
    @GetMapping("findById")
    public Result findById(Integer id) {
        Map<String, Object> map = orderService.findById(id);
        return new Result(true, MessageConst.QUERY_ORDER_SUCCESS, map);
    }
}
